package hu.plajko.client;

import hu.plajko.shared.rpc.NotificationRPC;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.DragAppearance;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GAETest implements EntryPoint {

	// private Label label = new Label();

	public void onModuleLoad() {

		final VLayout v = new VLayout();
		v.setWidth100();
		v.setHeight100();

		final DynamicForm form = new DynamicForm();

		TextAreaItem text = new TextAreaItem("text", "Text");
		text.setShowTitle(false);
		form.setFields(text);
		v.addMember(form);

		Button b = new Button("Create new note");
		b.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				NotificationPopup n = new NotificationPopup((String) form.getValue("text"));
				n.setLeft(300);
				n.setTop(300);
				n.setWidth(200);
				n.setHeight(200);
				n.draw();
				n.bringToFront();
				n.persist();
				// GreetingService.Client.getInstance().generate((String)
				// form.getValue("text"), new AsyncCallback<String>() {
				//
				// @Override
				// public void onSuccess(String result) {
				// label.setContents(result);
				//
				// }
				//
				// @Override
				// public void onFailure(Throwable caught) {
				// SC.say(caught.getMessage());
				//
				// }
				// });

			}
		});
		v.addMember(b);

		v.addMember(new Button("Remove all") {
			{
				addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						GreetingService.Client.getInstance().clearNotifications(new AsyncCallback<Void>() {

							@Override
							public void onFailure(Throwable arg0) {
								arg0.printStackTrace();
								SC.say(arg0.getMessage());

							}

							@Override
							public void onSuccess(Void arg0) {

							}
						});

					}
				});
			}
		});

		// label.setValign(VerticalAlignment.TOP);
		// label.setWidth("100%");
		// label.setHeight("100%");
		//
		// v.addMember(label);

		v.addMember(new Button("new style note") {
			{
				addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						new ExtendLayout() {
							{
								setLeft(Random.nextInt(800));
								setTop(Random.nextInt(600));
							}
						}.draw();

					}
				});
			}
		});

		v.draw();

		// new Timer() {
		// @Override
		// public void run() {
		// VLayout popup = getPopup("popup " + Random.nextInt());
		// popup.draw();
		// popup.animateShow(AnimationEffect.FADE);
		//
		// }
		// }.schedule(2000);

		v.setDisabled(true);
		GreetingService.Client.getInstance().getNotifications(new AsyncCallback<List<NotificationRPC>>() {

			@Override
			public void onFailure(Throwable caught) {
				SC.say(caught.getMessage());

			}

			@Override
			public void onSuccess(List<NotificationRPC> result) {
				if (result != null) {
					System.out.println(result.size() + " notifications loaded");
					for (NotificationRPC n : result)
						new NotificationPopup(n).draw();
				}
				v.setDisabled(false);
			}
		});

	}

	private VLayout getPopup(String text) {
		final VLayout popup = new VLayout();
		popup.setAutoHeight();
		popup.setAutoWidth();

		popup.setDragAppearance(DragAppearance.TARGET);
		popup.setEdgeImage("border.png");
		popup.setEdgeSize(18);
		popup.setShowEdges(true);
		popup.setEdgeShowCenter(true);
		popup.setCanDragReposition(true);
		popup.setCanDragResize(true);

		popup.addMember(new Label(text) {
			{
				setAutoHeight();
			}
		});
		popup.hide();

		popup.setTop(300);
		popup.setLeft(300);
		popup.setAnimateShowTime(1000);

		popup.addDoubleClickHandler(new DoubleClickHandler() {

			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				popup.animateResize(300, 300);

			}
		});
		return popup;

	}

}
